package com.kangnam.mongoTest;

import com.kangnam.mongo.MongoManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MongoManagerTest 
{

	private static MongoManager mongoManager = null;
	private static String DB_NAME;
	private static String P_INFO = "personalInfo";
	private static String E_P_INFO = "exPersonalInfo";
	
	public MongoManagerTest(String dbName, String host){
		mongoManager = MongoManager.getInstance();
		mongoManager.createPool(host);
		DB_NAME = dbName;	
	}
	
	/*
	 * {
	 * 		name : john,
	 * 		email: john@gmail.com,
	 * 		phone: 010-2211-1166
	 * }
	 */
	public void insertPersonalInfo(String name, String email, String phone){
		Mongo mongo = null;
		
		try {
			mongo = mongoManager.getConnection();
			System.out.println("insertPersonalInfo: " + mongo.hashCode());
			
			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(P_INFO);
			coll.drop();

			BasicDBObject query = new BasicDBObject();
			query.put("name", name);
			query.put("email", email);
			query.put("phone", phone);
			coll.insert(query);
			
		} finally {
			mongoManager.close(mongo);
		}
	}
	
	/*
	 * {
	 * 		name : john,
	 * 		email: john@gmail.com,
	 * 		phone: 010-2211-1166
	 * 		address: [
	 * 				{"office": "Seoul", "phone" : "02-111-1111"},
	 * 				  {home": "Gyeonggi", "phone" : "031-222-1111"}
	 * 		]
	 * }
	 */
	public void insertEmbeddedDoc(String name, String email, String phone){
		Mongo mongo = null;
		
		try {
			mongo = mongoManager.getConnection();
			System.out.println("insertEmbeddedDoc: " + mongo.hashCode());

			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(E_P_INFO);
			coll.drop();
			
			BasicDBObject query = new BasicDBObject();
			query.put("name", name);
			query.put("email", email);
			query.put("phone", phone);
						
			BasicDBObject address1 = new BasicDBObject();
			address1.put("office", "Seoul");
			address1.put("phone", "02-111-1111");
			
			BasicDBObject address2 = new BasicDBObject();
			address2.put("home", "Gyeonggi");
			address2.put("phone", "031-222-1111");

			BasicDBObject embed = new BasicDBObject();
			embed.put("$push", new BasicDBObject("address", address1));
			coll.update(query, embed, true, true);
			
			
			embed.put("$push", new BasicDBObject("address", address2));
			coll.update(query, embed, true, true);

		} finally {
			mongoManager.close(mongo);
		}
	}
	
	public static void viewCollection(String collection) {
		Mongo mongo = null;
		
		try{
			mongo = mongoManager.getConnection();
			System.out.println("viewCollection: " + mongo.hashCode());

			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(collection);
			DBCursor cursor = coll.find();
			while (cursor.hasNext()){
				DBObject dbObject = (DBObject) cursor.next();
				System.out.println(dbObject);
			}
		} finally {
			mongoManager.close(mongo);
		}
	}
	
	public static String getDocId(String collection, String field, String value){
		
		Mongo mongo = null;
		DBObject obj = null;
		
		try{
			mongo = mongoManager.getConnection();
			DB db = mongo.getDB(DB_NAME);
			
			BasicDBObject query = new BasicDBObject();
			query.append(field, value);
			
			DBCollection coll = db.getCollection(collection);		
			obj = coll.findOne(query);
//			DBCursor cur = coll.find(query);
			
			
		} finally {
			mongoManager.close(mongo);
		}
		return obj.get("_id").toString();
		
	}
	
	public static void main(String[] args) {
		MongoManagerTest mmt = new MongoManagerTest("testdb", "127.0.0.1");
		mmt.insertPersonalInfo("John", "john@gmail.com", "010-2211-1166");
		mmt.insertPersonalInfo("Mark", "mark@gmail.com", "010-2222-1166");
		mmt.insertPersonalInfo("Peter", "peter@gmail.com", "010-333-1166");
//
//		mmt.insertEmbeddedDoc("John", "john@gmail.com", "010-2211-1166");
//		mmt.insertEmbeddedDoc("Mark", "mark@gmail.com", "010-2222-1166");
//		mmt.insertEmbeddedDoc("Peter", "peter@gmail.com", "010-333-1166");
//
		mmt.viewCollection(P_INFO);
//		mmt.viewCollection(E_P_INFO);
		 
		String docId = mmt.getDocId(P_INFO, "name", "Peter");
		System.out.println(docId);
//		addBuylist(docId, productId);
	}

	

}
